package com.zzh.tools.utils;

import android.text.TextUtils;
import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-07-06
 * @desc
 */
public class ToolDateUtils {


    public static final String PATTERN_YM = "yyyyMM";

    public static final String PATTERN_YMD = "yyyy-MM-dd";

    public static final String PATTERN_YMDHM = "yyyy-MM-dd HH:mm";

    public static final String PATTERN_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_CN_YMD = "yyyy.MM.dd";

    public static final String PATTERN_OBLIQUE_YMD = "yyyy/MM/dd";

    public static String formatToData(String dataStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(dataStr);
        } catch (ParseException e) {
        }

        SimpleDateFormat sdfStr = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdfStr.format(date);

        return dateStr;
    }

    /**
     * 分月存储，避免一个文件夹太多
     *
     * @return yyyyMM
     */
    public static String getYearMonth() {
        return DateFormat.format(PATTERN_YM, new Date()).toString();
    }

    /**
     * 获取格式化后的日期字符串
     */
    public static String formatDate(long dateTime, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(dateTime);
        return df.format(date);
    }

    /**
     * 获取格式化后的日期字符串
     */
    public static String formatDate(String dateTime, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = df.parse(dateTime);
        return df.format(date);
    }

    /**
     * 获取格式化后的日期字符串
     */
    public static String formatDate(Date dateTime, String format) {
        return formatDate(dateTime.getTime(), format);
    }

    /**
     * 获取格式化后的日期对象
     */
    public static Date getDate(String dateStr, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.parse(dateStr);
    }

    /**
     * 获取格式化后的日期对象
     */
    public static Date getDate(long dateTime, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(dateTime);
        String newDate = df.format(date);
        return getDate(newDate, format);
    }

    /**
     * 获取格式化后的日期对象
     */
    public static Date getDate(Date date, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String newDate = df.format(date);
        return getDate(newDate, format);
    }


    /**
     * 根据需要自行截取或转换数据
     */
    public static Date getYearMonthDay(String dateStr) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_YMD);
        Date date = dateFormat.parse(dateStr);
        return date;
    }


    /**
     * 根据需要自行截取或转换数据
     */
    public static Date getYearMonthDay(Date date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_YMD);
        String dateString = dateFormat.format(date.getTime());
        Date newDate = dateFormat.parse(dateString);
        return newDate;
    }

    /**
     * 获取年月日小时分秒
     */
    public static String formatAccurateToSeconds(long dateTime) {
        return formatDate(dateTime, PATTERN_YMDHMS);
    }

    /**
     * 格式化时间
     */
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
        return format.format(date);
    }

    /**
     * 格式化时间 年/月/日
     */
    public static String formatDateOblique(String dataStr) throws ParseException {
        if (TextUtils.isEmpty(dataStr)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMD);
        Date date = null;
        try {
            date = sdf.parse(dataStr);
        } catch (ParseException e) {
        }
        if (date == null) {
            return dataStr;
        }

        SimpleDateFormat sdfStr = new SimpleDateFormat(PATTERN_OBLIQUE_YMD);
        String dateStr = sdfStr.format(date);

        return dateStr;
    }

    /**
     * 格式化时间
     */
    public static String formatDate(String dataStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMD);
        Date date = null;
        try {
            date = sdf.parse(dataStr);
        } catch (ParseException e) {
        }

        SimpleDateFormat sdfStr = new SimpleDateFormat(PATTERN_YMD);
        String dateStr = sdfStr.format(date);

        return dateStr;
    }

    //时间戳转字符串
    public static String getStrTime(String timeStamp) {
        try {
            return formatDate(timeStamp, PATTERN_YMDHMS);
        } catch (ParseException e) {
            ToolLog.e(e.toString());
        }
        return "";
    }

    private static SimpleDateFormat format1 = new SimpleDateFormat(PATTERN_YMD);

    public static String getStrTime1(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMDHMS);
        Date date = null;
        try {
            date = sdf.parse(timeStamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format1.format(date);
    }

    private static SimpleDateFormat format2 = new SimpleDateFormat(PATTERN_YMDHM);

    public static String getStrTime2(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMDHMS);
        Date date = null;
        try {
            date = sdf.parse(timeStamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format2.format(date);
    }

    private static SimpleDateFormat format3 = new SimpleDateFormat(PATTERN_CN_YMD);

    //2019.11.29
    public static String getStrTime3(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMDHMS);
        Date date = null;
        try {
            date = sdf.parse(timeStamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format3.format(date);
    }


    /**
     * 判断两天是不是同一天
     *
     * @param date1
     * @param date2
     * @return
     */

    public static boolean isSameDay(Date date1, Date date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(date1);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(date2);

        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 计算工时是不是有问题
     *
     * @param startDate 选择的开始时间
     * @param endDate   选择的结束时间
     * @param allHours  项目的总工时
     * @return -1 超出了24小时； -2 超出了12小时； 0 正常的没有问题
     */
    public static int calculateWorkingHours(Date startDate, Date endDate, int allHours) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date dayDate = calendar.getTime();
        boolean isSameDay = ToolDateUtils.isSameDay(dayDate, startDate);
        if (isSameDay) {
//            long selectAllMimutes = (endDate.getTime() - startDate.getTime() + (86400000 - (startDate.getTime() - dayDate.getTime()))) / 1000 / 60;
//            int allMimutest = allHours * 60;
//            if (allMimutest >= selectAllMimutes) { // 超过了24小时
//                return -1;
//            }
//
//            int dayMimutes = (int) (86400000 - (startDate.getTime() - dayDate.getTime())) / 1000 / 60;
//            int dayHours = dayMimutes / 60;
//
//            int selectAllHours = (int) (selectAllMimutes / 60);
//
//            if (dayHours <= 12) {
//                if (allHours - dayHours >= (selectAllHours - dayHours) / 2) {
//                    return -2; // 超过了12小时
//                }
//            } else {
//                if (allHours - 12 >= (selectAllHours - 12) / 2) {
//                    return -2; // 超过了12小时
//                }
//            }
            if (allHours >= 24) {
                return -1;
            } else if (allHours > 12) {
                return -2;
            }
        } else {
            long selectAllHours = (endDate.getTime() - startDate.getTime()) / 1000 / 60 / 60 + 24;
            if (allHours >= selectAllHours) { // 超过了24小时
                return -1;
            } else if (allHours > selectAllHours / 2) { // 超过了12小时
                return -2;
            }
        }
        return 0;
    }


}
